# Kernel config based on: arch/arm/configs/C8680_defconfig

pkgname="linux-gp-peak"
pkgver=3.0.21
pkgrel=2
pkgdesc="Peak kernel fork"
arch="armhf"
_carch="arm"
_flavor="gp-peak"
url="https://kernel.org"
license="GPL2"
options="!strip !check !tracedeps"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev"
HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

# Source
_repository="gp-peak-kernel"
_commit="f02b7a6f10b257b0452d3de47e19e55836e57613"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_commit.tar.gz::https://github.com/gp-b2g/${_repository}/archive/${_commit}.tar.gz
	$_config
	compiler-gcc6.h
	00_fix_return_address.patch
	01_timeconst_fix.patch
"
builddir="$srcdir/${_repository}-${_commit}"

prepare() {
	default_prepare

	# gcc6 support
	cp -v "$srcdir/compiler-gcc6.h" "$builddir/include/linux/"

	# Remove -Werror from all makefiles
	local i
	local makefiles="$(find . -type f -name Makefile)
		$(find . -type f -name Kbuild)"
	for i in $makefiles; do
		sed -i 's/-Werror-/-W/g' "$i"
		sed -i 's/-Werror//g' "$i"
	done

	# Prepare kernel config ('yes ""' for kernels lacking olddefconfig)
	cp "$srcdir"/$_config "$builddir"/.config
	yes "" | make ARCH="$_carch" HOSTCC="$HOSTCC" oldconfig
}

build() {
	unset LDFLAGS
	rm .config # Avoid complaints that the source is not clean
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-postmarketOS"
}

package() {
	# kernel.release
	install -D "$builddir/out/include/config/kernel.release" \
		"$pkgdir/usr/share/kernel/$_flavor/kernel.release"

	# zImage (find the right one)
	cd "$builddir/out/arch/$_carch/boot"
	_target="$pkgdir/boot/vmlinuz-$_flavor"
	for _zimg in zImage-dtb Image.gz-dtb *zImage Image; do
		[ -e "$_zimg" ] || continue
		msg "zImage found: $_zimg"
		install -Dm644 "$_zimg" "$_target"
		break
	done
	if ! [ -e "$_target" ]; then
		error "Could not find zImage in $PWD!"
		return 1
	fi

	# External modules install
	cd "$builddir"
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-postmarketOS" \
		INSTALL_MOD_PATH="$pkgdir" modules_install
}

sha512sums="e643530bc4e8540284820af137d300d8bf12fa352200095757a64e8e34a71d6e8892bf5887a988404864866751fb8fcac9e89b4429a76be59aeae670d04d533d  linux-gp-peak-f02b7a6f10b257b0452d3de47e19e55836e57613.tar.gz
577601feac4432540352d7ff147136d27e340f6a0798838deecbf9ae92f787afa6eb59e2ce0e08793e0912fccfc9e0e9f717069375555ac5e25978413736ce6c  config-gp-peak.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h
ea1d3b5a234fa565e3c1a792de48f4fc4e6023d281d303c8e319c7ef28edc5739ab0e4dea0139a41f0a5c7d03e27921ccaa214fd0ac5c72245a094ce60128864  00_fix_return_address.patch
a2bb98fb8d988bbb659cae00fbaca360828300e9b98b90aed5ee0dd839c3f740696df4094a9021b813cbada06820d115aabed581a47cdd2c947e8d853c20b145  01_timeconst_fix.patch"
